const db = require('../../MySql')

/**
 * 考试部门关联表
 */
class el_exam_depart {
    constructor(id, exam_id, depart_id) {
        this.id = id
        this.exam_id = exam_id
        this.depart_id = depart_id
    }

    /**
     * 根据exam_id查询
     */
    static async findExamDepartByExamId(examId) {
        const sql = `select * from el_exam_depart where exam_id = ?`
        const [result] = await db.query(sql, [examId])
        return result
    }

    /**
     * 根据depart_id查询
     */
    static async findExamDepartByDepartId(departId) {
        const sql = `select * from el_exam_depart where depart_id = ?`
        const [result] = await db.query(sql, [departId])
        return result
    }

    /**
     * 新增考试部门关联
     * @param {*} examId 考试ID
     * @param {*} departId 部门ID
     */
    static async addExamDepart(examId, departId) {
        if (departId.length === 0) {
            return
        }
        const sql = `INSERT INTO el_exam_depart (exam_id, depart_id) VALUES ?`
        const [result] = await db.query(sql, [examId, departId])
        return result
    }


    /**
     * 更新考试部门关联
     * @param {*} examId 考试ID
     * @param {*} departId 部门ID
     */
    static async updateExamDepart(examId, departId) {
        if (departId.length === 0) {
            return
        }
        const sql = `UPDATE el_exam_depart SET depart_id=? WHERE exam_id=?`
        const [result] = await db.query(sql, [departId, examId])
        return result
    }
}


module.exports = el_exam_depart